using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapRegionPanel : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapRegionPanel
        {
            #region ObjectBinding Generate

            public ExText name { protected set; get; }
            public GameObject regionItem { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExText>("name", out var __tbv0);
                name = __tbv0;
                __binding.TryGetVariableValue<GameObject>("regionItem", out var __tbv1);
                regionItem = __tbv1;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        #region fields

        private List<UIN_LevelMapRegionItem> _items;

        private int _curRegionCid;

        #endregion fields

        #region properties

        protected UIB_UIN_LevelMapRegionPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;
        public int CurRegion => _curRegionCid;
        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapRegionPanel();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            UIN_LevelMapRegionItem.onItemSelected += OnSelectedRegionChanged;
            _curRegionCid = GameplayAreaManager.Instance.curSceneArea.curRegionCid;
            _items = new List<UIN_LevelMapRegionItem>();
            AddAreaItem(ui.regionItem);
            ui.name.text = "地图";
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            _items.Clear();
            _items = null;
            UIN_LevelMapRegionItem.onItemSelected -= OnSelectedRegionChanged;
        }

        #region methonds

        public void Refresh(List<int> regions)
        {
            for (var i = 0; i < regions.Count; i++)
            {
                if (i == _items.Count)
                {
                    AddAreaItem(Object.Instantiate(ui.regionItem, ui.regionItem.transform.parent));
                }

                _items[i].Show();
                _items[i].Refresh(regions[i]);
                _items[i].UpdateSelect(_curRegionCid);
            }

            for (var i = regions.Count; i < _items.Count; i++)
            {
                _items[i].Hide();
            }
        }

        private void AddAreaItem(GameObject go)
        {
            var item = InitChildNode<UIN_LevelMapRegionItem>(go);
            _items.Add(item);
        }

        private void OnSelectedRegionChanged(int regionCid)
        {
            _curRegionCid = regionCid;
            if (belongWindow is UILevelMap uiLevelMap)
            {
                uiLevelMap.SwitchRegion(regionCid).Forget();
            }

            foreach (var item in _items)
            {
                item.UpdateSelect(regionCid);
            }

            UIMapManager.Instance.SetSelection(null);
        }

        #endregion methonds
    }
}